home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / network / ncsasock / sock_str.h < prev    next >
Text File  |  1996-07-05  |  5KB  |  155 lines

  1. /*
  2.  * BSD-style socket emulation library for the Mac
  3.  * Original author: Tom Milligan
  4.  * Current author: Charlie Reiman - creiman@ncsa.uiuc.edu
  5.  *
  6.  * This source file is placed in the public domian.
  7.  * Any resemblance to NCSA Telnet, living or dead, is purely coincidental.
  8.  *
  9.  *      National Center for Supercomputing Applications
  10.  *      152 Computing Applications Building
  11.  *      605 E. Springfield Ave.
  12.  *      Champaign, IL  61820
  13.  */
  14.  
  15. /*
  16.  * Internal Structures for the Toronto socket library
  17.  * Most of the complicated header stuff goes here.
  18.  */
  19.  
  20. #ifndef ipBadLapErr
  21. #include <MacTCPCommonTypes.h>
  22. #endif
  23. #ifdef ParamBlockHeader
  24. #undef ParamBlockHeader
  25. #endif
  26. #include <GetMyIPAddr.h>
  27. #include <TCPPB.h>
  28. #include <UDPPB.h>
  29. #include <AddressXlation.h>
  30.  
  31. #define TCPStateClosed         0
  32. #define TCPStateListen         2
  33. #define TCPStateSynReceived     4
  34. #define TCPStateSynSent         6
  35. #define TCPStateEstablished     8
  36. #define TCPStateFinWait1    10
  37. #define TCPStateFinWait2    12
  38. #define TCPStateCloseWait    14
  39. #define TCPStateClosing        16
  40. #define TCPStateLastAck        18
  41. #define TCPStateTimeWait    20
  42.  
  43. /* NUM_SOCKETS must be a power of 2 for the stream->socket hasing to work */
  44. #define NUM_SOCKETS            32        /* Number of sockets.  Should never exceed 32 */
  45. #define SOCKETS_MASK    (NUM_SOCKETS-1)  /* used for hash table wrap around via bitwise and */
  46. #define NUM_PBS            (NUM_SOCKETS+1)     /* number of pbs in global pool */
  47.  
  48. #define STREAM_BUFFER_SIZE     32768    /* memory for MacTCP streams */
  49.  
  50. #define UDP_MAX_MSG        65507    /* MacTCP max legal udp message */
  51. #define TCP_MAX_MSG        65535    /* MacTCP max legal tcp message */
  52.  
  53. #define TCP_MAX_WDS        4        /* arbitrary number of wds to alloc in sock_tcp_send */
  54.  
  55. /*
  56.  *    In use and shutdown status.
  57.  */
  58. #define    SOCK_STATUS_USED        0x1        /* Used socket table entry */
  59. #define    SOCK_STATUS_NOREAD        0x2        /* No more reading allowed from socket */
  60. #define    SOCK_STATUS_NOWRITE        0x4        /* No more writing allowed to socket */
  61.  
  62. /*
  63.  *    Socket connection states.
  64.  */
  65. #define SOCK_STATE_NO_STREAM    0    /* Socket doesn't have a MacTCP stream yet */
  66. #define    SOCK_STATE_UNCONNECTED    1    /* Socket is unconnected. */
  67. #define    SOCK_STATE_LISTENING    2    /* Socket is listening for connection. */
  68. #define    SOCK_STATE_LIS_CON        3    /* Socket is in transition from listen to connected. */
  69. #define    SOCK_STATE_CONNECTING    4    /* Socket is initiating a connection. */
  70. #define    SOCK_STATE_CONNECTED    5    /* Socket is connected. */
  71. #define SOCK_STATE_CLOSING      6   /* Socket is closing */
  72.  
  73. typedef union AllPb 
  74.     {
  75.     TCPiopb        tcp;
  76.     UDPiopb        udp;
  77.     } AllPb;
  78.     
  79. typedef struct SocketRecord 
  80. {
  81.     StreamPtr            stream;        /* stream pointer */
  82.     byte                status;        /* Is file descriptor in use */
  83.     int                    fd;            /* fd number */
  84.     short                protocol;    /* Protocol (e.g. TCP, UDP) */
  85.     Boolean                nonblocking;/* socket set for non-blocking I/O. */
  86.     char                *recvBuf;    /* receive buffer */
  87.     int                    recvd;        /* amount received */
  88.     int                    torecv;        /* amount to receive */
  89.     struct sockaddr_in    sa;            /* My address. */
  90.     struct sockaddr_in    peer;        /* Her address. */
  91.     byte                sstate;        /* socket's connection state. */
  92.     unsigned long        dataavail;    /* Amount of data available on connection. */
  93.     int                    asyncerr;    /* Last async error to arrive.  zero if none. */
  94.     /* stdio stuff */
  95. #ifndef  DONT_INCLUDE_SOCKET_STDIO
  96.     char                *outbuf;    /* Ptr to array to buffer output */
  97.     int                    outbufcount;/* # of characters in outbuf */
  98.     Ptr                    outbufptr;    /* Pointer into outbuf */
  99.     char                *inbuf;        /* Ptr to array to buffer input */
  100.     int                    inbufcount;    /* # of characters in inbuf */
  101.     Ptr                    inbufptr;    /* Pointer into inbuf */
  102.     Boolean                ioerr;        /* Holds error status for stdio calls */
  103.     Boolean                ioeof;        /* EOF was detected on stream */
  104. #endif
  105. } SocketRecord, *SocketPtr;
  106.  
  107. /*
  108.  * Quick note for hash table: 
  109.  *  Stream =  0 => unused
  110.  *  Stream = -1 => deleted
  111.  *  Stream = anything else => stream ptr
  112.  */
  113. typedef struct StreamHashEnt
  114.     {
  115.     StreamPtr    stream;
  116.     SocketPtr    socket;
  117.     } StreamHashEnt, *StreamHashEntPtr;
  118.  
  119. typedef    struct    miniwds
  120.     {
  121.     unsigned short length;
  122.     char * ptr;
  123.     unsigned short terminus;    /* must be zero'd for use */
  124.     } miniwds;
  125.  
  126. #ifndef __socket_ext__
  127. /*typedef int (*SpinFn)(spin_msg msg,long param);*/
  128. #endif /* __socket_ext__ */
  129.  
  130. /*-------------------------------------------------------------------------*/
  131. #define        SOCK_MIN_PTR            (Ptr)0x1400        /* Minimum reasonable pointer */
  132. #define        goodptr(p)                (((Ptr) p) > SOCK_MIN_PTR)
  133. #define        is_used(p)                (goodptr(p) && (p)->status & SOCK_STATUS_USED)
  134. #define        is_stdio(p)                (is_used(p) && (p)->inbuf != NULL)
  135. #define        sock_good_fd(s)            ((0 <= s && s < NUM_SOCKETS) && is_used (sockets+s))
  136. #define        sock_nowrite(p)            ((p)->status & SOCK_STATUS_NOWRITE)
  137. #define        sock_noread(p)            ((p)->status & SOCK_STATUS_NOREAD)
  138.  
  139. #define     TVTOTICK(tvsec,tvusec)    ( ((tvsec)*60) + ((tvusec)/16666) )
  140. #define        min(a,b)                ( (a) < (b) ? (a) : (b))
  141. #define        max(a,b)                ( (a) > (b) ? (a) : (b))
  142.  
  143. /* SPIN returns a -1 on user cancel for fn returning integers */
  144. #define        SPIN(cond,mesg,param)    do {if (spinroutine)\
  145.                                     if ((*spinroutine)(mesg,param))\
  146.                                         return -1;\
  147.                                     }while(cond);
  148.  
  149. /* SPINP returns a NULL on user cancel, for fn returning pointers */                
  150. #define        SPINP(cond,mesg,param)    do {if (spinroutine)\
  151.                                     if ((*spinroutine)(mesg,param))\
  152.                                         return NULL;\
  153.                                     }while(cond);
  154.                                     
  155.